#include "func_password.h"

void get_salt(char *salt, char *passwd) {
	int i, j;
	for(i = 0, j = 0; passwd[i] && j != 3; ++i){
		if(passwd[i] == '$') ++j;
	}
	strncpy(salt, passwd, i - 1);
}

int func_password(int peerfd, char *username)
{
    char salt[512] = {0};
    char passwd[256] = {0};
    struct spwd *sp;
    if((sp = getspnam(username)) == NULL) {
        perror("getspnam");
        close(peerfd);
        return -1;
    }
    puts(sp->sp_pwdp);
    get_salt(salt, sp->sp_pwdp);
    puts(salt);
    send(peerfd, salt, strlen(salt), 0);
    puts("salt send");
    recv(peerfd, passwd, sizeof(passwd), 0);
    if(strcmp(passwd, sp->sp_pwdp)){
        close(peerfd);
    }
    return 0;
}

